package domain.entities;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for
 * CdaMolecula entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see domain.entities.CdaMolecula
 * @author MyEclipse Persistence Tools
 */

public class CdaMoleculaDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(CdaMoleculaDAO.class);

	// property constants

	protected void initDao() {
		// do nothing
	}

	public void save(CdaMolecula transientInstance) {
		log.debug("saving CdaMolecula instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(CdaMolecula persistentInstance) {
		log.debug("deleting CdaMolecula instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public CdaMolecula findById(java.lang.Integer id) {
		log.debug("getting CdaMolecula instance with id: " + id);
		try {
			CdaMolecula instance = (CdaMolecula) getHibernateTemplate().get(
					"domain.entities.CdaMolecula", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

    public CdaMolecula findByCentroId(java.lang.Integer id) {

        try {
            String queryString = "select m from CdaMolecula m where m.involucrado.id =" + id;
            List<CdaMolecula> list = getHibernateTemplate().find(queryString);
            if(list.size() > 0)
                return list.get(0);
            else
                return null;

        } catch (RuntimeException re) {

            throw re;
        }
    }

    public List<CdaMolecula> findByInvolucradoId(java.lang.Integer id) {
        String queryString = "select m from CdaMolecula m where m.involucrado.id =" + id;
        List<CdaMolecula> list = getHibernateTemplate().find(queryString);
        if(list.size() > 0)
            return list;
        else
            return new ArrayList<CdaMolecula>();
    }


    public List<CdaMolecula> findByExample(CdaMolecula instance) {
		log.debug("finding CdaMolecula instance by example");
		try {
			List<CdaMolecula> results = (List<CdaMolecula>) getHibernateTemplate()
					.findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding CdaMolecula instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from CdaMolecula as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all CdaMolecula instances");
		try {
			String queryString = "from CdaMolecula";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public CdaMolecula merge(CdaMolecula detachedInstance) {
		log.debug("merging CdaMolecula instance");
		try {
			CdaMolecula result = (CdaMolecula) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(CdaMolecula instance) {
		log.debug("attaching dirty CdaMolecula instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(CdaMolecula instance) {
		log.debug("attaching clean CdaMolecula instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

 /*   public List findCdaMoleculas(Integer cdaMoleculaid) {
        try {
            String queryString = "";
            if( cdaMoleculaid != null)
                 queryString = "select m from CdaMolecula m inner join m.involucrado i where i.centro.id = " + centroid + " and m.id <> " + cdaMoleculaid;
            else
                 queryString = "select m from CdaMolecula m inner join m.involucrado i where i.centro.id = " + centroid;

            return getHibernateTemplate().find(queryString);
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }        */

    public void updateActivo( Integer involucradoid) {

        try {

      /*      for(CdaMolecula c : list){

                     String query2 = "update CdaMolecula m set m.activo = false where id = " + c.getId() ;

                    Query query = getHibernateTemplate().getSessionFactory().openSession().createQuery(query2);

                    query.executeUpdate();

            }      */

            String queryString = "update CdaMolecula m set m.activo = false where m.involucrado = " + involucradoid;

            Query query = getSession().createQuery(queryString);
            query.executeUpdate();


        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

	public static CdaMoleculaDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (CdaMoleculaDAO) ctx.getBean("CdaMoleculaDAO");
	}
}